var menushow = false;
var breakername;
// 断路器map
var breakerMap = [];
// 变压器map
var transformerMap = [];
// 保护map
var protectMap = [];
$(function() {

	var sid = $("#sid").val();
	$("#maindiv").append($("#svgtext").val());
	$("svg").attr("id", "svgid");
	var svg = Snap("#svgid");
	if (svg) {
		svg.drag();
	}
	$("text").click(function(e) {
		var txt = $(this).text();
		judgeType(txt);
		selectname = txt;
		$('#selectText').text(txt);
		$('#mainmenu').css("left", e.clientX + 10);
		$('#mainmenu').css("top", e.clientY + 5);
		$('#mainmenu').css("display", "block");
		menushow = true;
	});
	$("#svgid").click(function(e) {
		if (menushow) {
			menushow = false;
		} else {
			$('#mainmenu').css("display", "none");
		}
	});
	getBreaker(sid);
	getProtect(sid);
	getTransform(sid);

});
// 断路器
// 熄弧分析
function arcFenxi() {
	openArcDlg(selectname);
}
// 基本信息
function breakerbaseInfo() {
	openBreakerBaseInfoDlg(selectname);
}
// 风险评估
function breakerRiskEva() {
	openBreakerRiskEvaDlg(selectname);
}
// 变压器
// 基本信息
function transformerBaseInfo() {
	openTransformerBaseInfoDlg(selectname);
}
// 风险评估
function transformerRiskEva() {
	openTransformerRiskEvaDlg(selectname);
}
// 保护动作分析
function transformerProtectFenxi() {
	openTransformerProtectFenxiDlg(selectname);
};
// 保护
// 基本信息
function protectBaseInfo() {
	openProtectBaseInfoDlg(selectname);
}
// 风险评估
function protectRiskEva() {
	openProtectRiskEvaDlg(selectname);
}
// 保护动作分析
function protectFenxi() {
	openProtectDlg(selectname);
}
// CT饱和特性分析

function ctFeatureFenxi() {
	openCtDlg();
}
function judgeType(txt) {
	$('#menu').empty();
	for ( var i in breakerMap) {
		if (txt == breakerMap[i][1]) {
			var a = $("<a class='btn btn-block btn-social btn-twitter' id='breakerBaseInfo' onclick='breakerbaseInfo()'> <i class='fa fa-caret-right'></i>基础信息</a> <a class='btn btn-block btn-social btn-twitter' onclick='breakerRiskEva()'> <i class='fa fa-caret-right' ></i>风险评估</a> <a class='btn btn-block btn-social btn-twitter' href='javascript:void(0);' id='arcFenxi' onclick='arcFenxi();'> <i class='fa fa-caret-right'></i> 断路器熄弧分析</a> <a class='btn btn-block btn-social btn-twitter' href='javascript:void(0);' onclick='ctFeatureFenxi();'> <i class='fa fa-caret-right'></i> CT饱和特性分析</a>");
			$('#menu').append(a);
			return;
		}
	}
	for ( var i in transformerMap) {
		if (transformerMap[i][1].indexOf(txt) > -1) {
			var a = $("<a class='btn btn-block btn-social btn-twitter' id='transformerBaseInfo' onclick='transformerBaseInfo()'> <i class='fa fa-caret-right'></i>基础信息</a> <a class='btn btn-block btn-social btn-twitter' onclick='transformerRiskEva()'> <i class='fa fa-caret-right'></i>风险评估</a> <a class='btn btn-block btn-social btn-twitter' href='javascript:void(0);' id='protectFenxi' onclick='transformerProtectFenxi()' > <i class='fa fa-caret-right'></i> 保护动作分析</a>");
			$('#menu').append(a);
			return;
		}
	}
	for ( var i in protectMap) {
		for ( var j in protectMap[i].value) {
			if (txt.indexOf(protectMap[i].value[j][2]) > -1) {
				var a = $("<a class='btn btn-block btn-social btn-twitter' onclick='protectBaseInfo()'> <i class='fa fa-caret-right'></i>基础信息</a> <a class='btn btn-block btn-social btn-twitter' onclick='protectRiskEva()'> <i class='fa fa-caret-right'></i>风险评估</a> <a class='btn btn-block btn-social btn-twitter' href='javascript:void(0);' id='protectFenxi' onclick='protectFenxi();'> <i class='fa fa-caret-right'></i> 保护动作分析</a>");
				$('#menu').append(a);
				return;
			}

		}
	}
	var a = $("<a class='btn btn-block btn-social btn-twitter' href='javascript:void(0);'> <i class='fa fa-caret-right'></i>基础信息</a> ");
	$('#menu').append(a);

}
// 断路器
// 熄弧分析
function openArcDlg(breakername) {
	var breakerId;
	// 根据断路器名称找到相应的id
	for ( var i in breakerMap) {
		if (breakername == breakerMap[i][1]) {
			breakerId = breakerMap[i][0];
			break;
		}
	}
	MyModal.showModal("/ui/map/dialog/detailedBreaker.xhtml?breakerName="
			+ breakername + "&breakerId=" + breakerId);
}
// 基本信息
function openBreakerBaseInfoDlg(breakername) {
	var breakerId;
	// 根据断路器名称找到相应的id
	for ( var i in breakerMap) {
		if (breakername == breakerMap[i][1]) {
			breakerId = breakerMap[i][0];
			break;
		}
	}
	MyModal.showModal("/ui/map/dialog/breakerBaseInfo.xhtml?breakerName="
			+ breakername + "&breakerId=" + breakerId);
}

// 风险评估
function openBreakerRiskEvaDlg(breakername) {
	var breakerId;
	// 根据断路器名称找到相应的id
	for ( var i in breakerMap) {
		if (breakername == breakerMap[i][1]) {
			breakerId = breakerMap[i][0];
			break;
		}
	}
	MyModal.showModal("/ui/map/dialog/breakerRiskEva.xhtml?breakerName="
			+ breakername + "&breakerId=" + breakerId);
}

// 变压器
// 基本信息
function openTransformerBaseInfoDlg(transformername) {
	var transformerId;
	// 根据变压器名称找到相应的id
	for ( var i in transformerMap) {
		if (transformername == transformerMap[i][1]) {
			transformerId = transformerMap[i][0];
			break;
		}
	}
	MyModal
			.showModal("/ui/map/dialog/transformerBaseInfo.xhtml?transformerName="
					+ transformername + "&transformerId=" + transformerId);
}

// 风险评估
function openTransformerRiskEvaDlg(transformername) {
	var transformerId;
	// 根据变压器名称找到相应的id
	for ( var i in transformerMap) {
		if (transformername == transformerMap[i][1]) {
			transformerId = transformerMap[i][0];
			break;
		}
	}
	MyModal
			.showModal("/ui/map/dialog/transformerRiskEva.xhtml?transformerName="
					+ transformername + "&transformerId=" + transformerId);
}
// 保护动作分析
function openTransformerProtectFenxiDlg(transformername) {
	var transformerId;
	// 根据变压器名称找到相应的id
	for ( var i in transformerMap) {
		if (transformername == transformerMap[i][1]) {
			transformerId = transformerMap[i][0];
			break;
		}
	}
	MyModal
			.showModal("/ui/map/dialog/transformerProtectFenxi.xhtml?transformerName="
					+ transformername + "&transformerId=" + transformerId);
}

// 保护
// 基本信息
function openProtectBaseInfoDlg(protectname) {
	var protectId;
	// 根据保护名称找到相应的id
	for ( var i in protectMap) {
		for ( var j in protectMap[i].value) {
			if (protectMap[i].value[j][2] == protectname) {
				protectId = protectMap[i].value[j][0];
				break;
			}
		}
	}
	MyModal.showModal("/ui/map/dialog/protectBaseInfo.xhtml?protectName="
			+ protectname + "&protectId=" + protectId);
}
// 风险评估
function openProtectRiskEvaDlg(protectname) {
	var protectId;
	// 根据保护名称找到相应的id
	for ( var i in protectMap) {
		for ( var j in protectMap[i].value) {
			if (protectMap[i].value[j][1].indexOf(protectname) > -1) {
				protectId = protectMap[i].value[j][0];
				break;
			}
		}
	}
	MyModal.showModal("/ui/map/dialog/protectRiskEva.xhtml?protectName="
			+ protectname + "&protectId=" + protectId);
}
// 保护动作分析
function openProtectDlg(protectname) {
	var protectId;
	// 根据保护名称找到相应的id
	for ( var i in protectMap) {
		for ( var j in protectMap[i].value) {
			if (protectMap[i].value[j][1].indexOf(protectname) > -1) {
				protectId = protectMap[i].value[j][0];
				break;
			}
		}
	}
	MyModal.showModal("/ui/map/dialog/protectActFenxi.xhtml?protectName="
			+ protectname + "&protectId=" + protectId);
}
// CT饱和特性分析
function openCtDlg() {
	MyModal.showModal("/ui/map/dialog/ctFeatureFenxi.xhtml");
}

// 页面加载完就获取本站所有的断路器名字和id
function getBreaker(sid) {
	$.getJSON("/rest/cim/getBreakersStationId/" + sid, function(result) {
		breakerMap = result;
	});
}
// 获取本站所有的保护名字和id
function getProtect(sid) {
	$.getJSON("/rest/cim/getProtectsStationId/" + sid, function(result) {
		protectMap = result;
	});
}
// 获取本站所有的变压器名字和id
function getTransform(sid) {
	$.getJSON("/rest/cim/getTransformByStationId/" + sid, function(result) {
		transformerMap = result;
	});
}
